package com.yix.jruby.process;

import lombok.extern.slf4j.Slf4j;
import org.jruby.Ruby;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedDeque;

@Slf4j
public class JRubyExecutor {

    public static Queue<Ruby> rubyPool = new ConcurrentLinkedDeque<>();
    protected Ruby ruby;
    public JRubyExecutor(){
        //  从池子获取
        log.info("获取ruby");
        Ruby ruby = rubyPool.poll();
        if(ruby==null){
            ruby = Ruby.newInstance();
        }
        this.ruby = ruby;
    }
    /**
     * 关闭执行器并回收资源（不回收的话下次启动实例会变慢，因为ruby解释器不回到池子里
     */
    public void close(){
        if(ruby!=null) {
            rubyPool.add(ruby);
        }
        ruby = null;
    }

    public Ruby get() {
        return ruby;
    }

}
